/**
 * Project: report2imolike
 * File name: gui.cpp
 * Description:  This class provides GUI for all project
 *
 * @author Vladimir Burgić, Petnica Meteor Group http://meteori.rs, Copyright (C) 2011.
 * @version 0.2
 *
 * @see The GNU Public License 3 (GPL)
 */
/*
 *   This file is part of report2imolike project.
 *
 *   Report2imolike is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Report2imolike is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with report2imolike.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <QApplication>
#include <QString>
#include <QStringList>
#include <QFileDialog>
#include <QMessageBox>
#include <QIcon>
#include "gui.h"


Gui::Gui(QWidget *parent) :
    QDialog(parent)
{
    baseDir = QDir::homePath();
    itsTransformer = new Transformer(this);
    connect (itsTransformer, SIGNAL(parsingOutcome(int)), this, SLOT(parseErrors(int)));

    //GUI setup
    ui.setupUi(this);
    qApp->setWindowIcon(QIcon("./report2imolike.ico"));

    ui.previewButton->setDisabled(true);
    ui.saveButton->setDisabled(true);
    ui.infixLineEdit->setDisabled(true);
    ui.useDefaultInfixCheckBox->setChecked(false);

    connect (ui.useDefaultInfixCheckBox, SIGNAL(clicked(bool)), ui.infixLineEdit, SLOT(setEnabled(bool)));
    connect (ui.useDefaultInfixCheckBox, SIGNAL(clicked()), ui.infixLineEdit, SLOT(setFocus()));

    connect (itsTransformer, SIGNAL(parsable2string(bool)), ui.previewButton, SLOT(setEnabled(bool)));
    connect (itsTransformer, SIGNAL(parsable2file(bool)), ui.saveButton, SLOT(setEnabled(bool)));

    //Giving functionality to gui
    connect (ui.inputFilesButton, SIGNAL(clicked()), this, SLOT(getInputFileNames()));

    connect (ui.outputDirButton, SIGNAL(clicked()), this, SLOT(getOutputDirName()));
    connect (ui.outputDirLineEdit, SIGNAL(textChanged(QString)), itsTransformer, SLOT(setOutputDir(QString)));

    connect (ui.solarLongitudeButton, SIGNAL(clicked()), this, SLOT(getSolLongFileName()));
    connect (ui.solarLongitudeLineEdit, SIGNAL(textChanged(QString)), itsTransformer, SLOT(setSolLongFile(QString)));

    connect (ui.previewButton, SIGNAL(clicked()), this, SLOT(previewData()));
    connect (ui.saveButton, SIGNAL(clicked()), this, SLOT(exportData()));

    connect (ui.cancelButton, SIGNAL(clicked()), qApp, SLOT(quit()));
    connect (ui.aboutButton, SIGNAL(clicked()), this, SLOT(showAbout()));
    connect (ui.aboutQtButton, SIGNAL(clicked()), qApp, SLOT(aboutQt()));

    // seting up infix edit
    defaultInfix = tr("_REP_");
    itsTransformer->setInfix(defaultInfix);
    ui.infixLineEdit->setText(defaultInfix);
    connect (ui.infixLineEdit, SIGNAL(textChanged(QString)), itsTransformer, SLOT(setInfix(QString)));
    connect (ui.useDefaultInfixCheckBox, SIGNAL(toggled(bool)), this, SLOT(resetInfix(bool)));

    //someone may find it easier to change text in lineedits for sollongfilename and output dir:
    connect(ui.solarLongitudeLineEdit, SIGNAL(textChanged(QString)), itsTransformer, SLOT(setSolLongFile(QString)));
    connect(ui.outputDirLineEdit, SIGNAL(textChanged(QString)), itsTransformer, SLOT(setOutputDir(QString)));
    //

    //set up Preview
    itsPreview = new Preview(this);
    itsPreview->saveButtonEnabled(false);
    connect (itsTransformer, SIGNAL(parsable2file(bool)), itsPreview, SLOT(saveButtonEnabled(bool)));
    connect (itsPreview, SIGNAL(saveFiles()), ui.saveButton, SIGNAL(clicked()));

}

void Gui::getInputFileNames()
{
    QStringList inputFileNames = QFileDialog::getOpenFileNames(
            this,
            tr("Open Document(s)"),
            baseDir,
            tr("Text files (*.txt);;All files (*.*)"));
    if (!inputFileNames.isEmpty()){
        itsTransformer->setInputFiles(inputFileNames);
        ui.inputFilesListWidget->clear();
        ui.inputFilesListWidget->addItems(inputFileNames);
        baseDir = inputFileNames.at(0);
    }
}

void Gui::getOutputDirName()
{
    QString outputDir = QFileDialog::getExistingDirectory(
            this,
            tr("Select Output Folder"),
            baseDir);
    if (!outputDir.isEmpty()){
        itsTransformer->setOutputDir(outputDir);
        ui.outputDirLineEdit->setText(outputDir);
        baseDir = outputDir;
    }
}


void Gui::getSolLongFileName()
{
    QString solLongFileName = QFileDialog::getOpenFileName(
            this,
            tr("Open Document"),
            baseDir,
            tr("Text files (*.txt);;All files (*.*)"));
    if (!solLongFileName.isEmpty()){
        itsTransformer->setSolLongFile(solLongFileName);
        ui.solarLongitudeLineEdit->setText(solLongFileName);
        baseDir = solLongFileName;
    }
}

void Gui::resetInfix(bool useCustomInfix)
{
    if (!useCustomInfix)
        ui.infixLineEdit->setText(defaultInfix);
}

void Gui::previewData()
{
    QStringList rateLineList, magnLineList;
    itsTransformer->convert(rateLineList, magnLineList);

    //show preview!
    itsPreview->showRatePreview(rateLineList);
    itsPreview->showMagnPreview(magnLineList);
    itsPreview->show();
}

void Gui::exportData()
{
    itsTransformer->convert();
}

void Gui::parseErrors(int error)
{
    switch (error){
    case Transformer::OK: QMessageBox::information(this, tr("report2imolike"), tr("Conversion successful"), QMessageBox::Ok, QMessageBox::Ok);
        break;
    case Transformer::BadFileFormat: QMessageBox::warning(this, tr("report2imolike"), tr("Bad File Format"), QMessageBox::Ok, QMessageBox::Ok);
        break;
    case Transformer::UnaccessibleInputFile: QMessageBox::warning(this, tr("report2imolike"), tr("Input file could not be read!"), QMessageBox::Ok, QMessageBox::Ok);
        break;
    case Transformer::UnaccessibleOutputDir: QMessageBox::warning(this, tr("report2imolike"), tr("Files could not be written into selected folder"), QMessageBox::Ok, QMessageBox::Ok);
        break;
    case Transformer::NoSolLongFile: QMessageBox::warning(this, tr("report2imolike"), tr("No or bad file with solar longitudes"), QMessageBox::Ok, QMessageBox::Ok);
        break;
    default: QMessageBox::warning(this, tr("report2imolike"), tr("Something happened?!"), QMessageBox::Ok, QMessageBox::Ok);
        break;
    }
}

void Gui::showAbout()
{
    QString aboutText = "<h2>report2imolike</h2>";
    aboutText += "<p><b>Version:</b> 0.2.1</p>";
    aboutText += "<p><b>Author:</b> <a href=\"http://www.meteori.rs\">Petnica Meteor Group (http://meteori.rs)</a></p>";
    aboutText += "<p><b>Licence:</b> GNU General Public Licence v3 (<a href=\"http://www.gnu.org/licenses/gpl.html\">http://www.gnu.org/licenses/gpl.html</a>)</p>";
    aboutText += "<p>For <b>help and documentation</b> please visit <a href=\"http://code.google.com/p/report2imolike/\">http://code.google.com/p/report2imolike/</a></p>";
    aboutText += "<p>This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.</p>";
    QMessageBox::about(this, "About report2imolike", aboutText);
}
